﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
using WPFExcelReport.Helpers;
using System.Windows.Input;
using System.Windows.Controls;
namespace WPFExcelReport
{
    public partial class TGB15
    {

        public TGB15()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGLLG();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if(dwMaster==null)
            {

                KEY = "";
            }
            else
            {
                KEY = TMV.GLLBEH;
            }
            txtLG.Focus();
        }

        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            //RWAUF = RWKRS + RWLFD;
            base.QueryCommand(sender, e);
        }

        public void Copy(DataRow _dwWB)
        {
            if (_dwWB == null)
                return;
            if (NBOX == 0)
                return;
            DataRow dwData;
            while (NBOX > 1)
            {
                dwData = TBMain.NewRow();
                dwData.ItemArray = _dwWB.ItemArray;
                LFD = VSDDAT.SD22.GETNEXT("0022", "AB", "GLLBEHI", 1, 10);
                dwData["GLLBEH"] = LFD;
                dwData["GLLBEHI"] = dwData["GLLBEH"];
                TBMain.Rows.InsertAt(dwData, 0);
                TMV.InsertGLLG(dwData, app.company);
                TMV.GBW.NWBBEW(dwData, "10", RUNID);
                BOX--;
            }
            //多增加一个虚拟的
            dwData = TBMain.NewRow();
            dwData.ItemArray = _dwWB.ItemArray;
            LFD = VSDDAT.SD22.GETNEXT("0022", "AB", "GLLBEHI", 1, 10);
            dwData["GLLBEH"] = LFD;
            dwData["GLLBEHI"] = dwData["GLLBEH"];
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
        }

        public void Entry(DataRow _dwWB)
        {
            string _time = "";
            string _date = "";
            Helpers.Helper.GetTime(ref _date, ref _time);

            _dwWB["GLUBDAT"] = _date;
            _dwWB["GLUBZEIT"] = _time;

            TMV.InsertGLLG(_dwWB, app.company);
            TMV.GBW.NWBBEW(_dwWB, "10", RUNID);
            PM["PARTIE"] = _dwWB["GLPARTIE"].ToString();
            PM["PARTIEKZ"] = _dwWB["GLPARTIEKZ"].ToString();

            PM["WH"] = _dwWB["GLLO"].ToString() + _dwWB["GLLB"].ToString() + _dwWB["GLLP"].ToString();
            PM["ORDERNUM"] = "".PadRight(20);
            PM["AUFTYP"] = "".PadRight(1);
            TMV.VGP.BOXCHG(PM);
            TMV.VGB.BOXCHG(PM);
        }

        private void QTY_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key != System.Windows.Input.Key.Enter)
                    return;
                if (e.Key != System.Windows.Input.Key.Return)
                    return;
                if (!IsEdit)
                    return;
                if (!IsKEY)
                    return;
                TextBox txt = sender as TextBox;
                if (dwMaster == null)
                {
                    MessageBox.Show("请先选择一个布卷。");
                    return;
                }
                dwMaster["GLBSTNET"] = System.Convert.ToDouble(txt.Text) * 1000;
                dwMaster["GLBSTKG"] = dwMaster["GLBSTNET"];
                Entry(dwMaster);//插入
                Copy(dwMaster);//复制并插入
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            TMV.GBW.POSTCHG();
            TMV.BATCHWBCHG();
            base.SaveCommand(sender, e);
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                MessageBox.Show("已处于编辑状态，请先将数据保存后才可再次进行编辑！");
                return;
            }

            MV.TBTemp = null;
            MV.TBTemp = TBMain.Copy();
            base.EditCommand(sender, e);
            IsEdit = true;
        }

        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;

            string GLLBEH = dwMaster["GLLBEH"].ToString();
            vwMaster.View.MoveCurrentTo(-1);
            filter = "GLLBEH='" + GLLBEH + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                TMV.DGLLG01(row);
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        public override void CopyCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            Copy();
            base.CopyCommand(sender, e);
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 8)
                return;
            PARTIE = EXAUF.Substring(0, 7);
            SResult = TMV.GBDLOAD(EXAUF.Substring(0,7), EXAUF.Substring(7,1));
            MergeData(SResult, true);
            TBGB = null;
            TBGB = TBMain;
            if (!IsAuto)
                return;
            if (Counts > 0)
                return;
            PM["PARTIE"] = EXAUF.Substring(0, 7);
            PM["KZ"] = EXAUF.Substring(7, 1);
            PM["LO"] = LO;
            PM["LB"] = LB;
            PM["LP"] = LP;
            PM["ART"] = CART.Substring(0,20);
            PM["VAR"] = CART.Substring(20, 3);
            PM["FARBE"] = CART.Substring(23, 10);
            PM["BOX"] = BOX;
            PM["QTY"] = QTY*0.01;
            PM["MU"] = MU;
            DataTable tb = TMV.NEWMBOX(PM);
            TBMain.Clear();
            TBMain.Merge(tb);
            NBOX = 0;
            vwMaster.View.MoveCurrentToFirst();
            base.OnEXAUFChg(vlaue);
        }
       
        public override void LOADBYAUF()
        {
            if (ORDERNUM.Length == 0)
                return;
            filter = "GLLBEH='" + ORDERNUM + "'";
            rows = TBMain.Select(filter);

            foreach (DataRow dwm in rows)
            {
                dwData = TBMain.NewRow();
                dwData.ItemArray = dwm.ItemArray;
                dwData["GLLBEH"] = " AND GLLBEH='" + ORDERNUM + "'";
                SResult = TMV.QGLLG10(dwData);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwm.ItemArray = row.ItemArray;
                }
                break;
            }
            base.LOADBYAUF();
        }


    }
}
